<!-- 新版评吸设置·1 -->
<template>
  <div class="setting_new">
    <div class="setting_new_top_operator df jc-sb">
      <div></div>
      <!-- <div><lay-button size="sm" @click="close">关闭</lay-button></div> -->
    </div>
    <lay-table :height="'100%'" :columns="columns" :default-toolbar="false" :data-source="dataSource">

      <template v-slot:toolbar>
        <div class="df jc-sb tab_top_btn">
          <div>
            <lay-button type="primary" size="sm" @click="sing_open" v-permissions="'setting_sing'">{{signName}}</lay-button>
            <lay-button type="normal" size="sm" v-permissions="'exp_sign'" v-if="title==='查看任务'" @click="exportSign">导出签到表</lay-button>
          </div>
          <div>
            <lay-button type="normal" size="sm" @click="close_px" v-if="title=='开始任务'">结束评吸</lay-button>
            <lay-button size="sm" @click="close">关闭</lay-button>
          </div>
        </div>
      </template>

      <template v-slot:classify="{ row }">
        <span>{{ taskArr[row.classify] }}</span>
      </template>
      <template v-slot:status="{ row }">
        <!-- <span>{{  row.status==0?'未开始': item.status==1?'进行中':'已完成' }}</span> -->
        <el-tag :type="row.status==0?'danger': row.status==1?'primary':'success'">{{  row.status==0?'未开始': row.status==1?'进行中':'已完成' }}</el-tag>
      </template>
      <template v-slot:operator="{ row }">
        <lay-button size="xs" type="primary" @click="start_task(row)" v-permissions="'task_start'" v-if="title!='评吸烟支设置'  && row.status==0">开始任务</lay-button>

        <lay-button size="xs" type="primary" @click="smoke_setting(row)" v-permissions="'metting_opt'" v-if="title==='评吸烟支设置' ">配置任务</lay-button>
        <lay-button size="xs" type="danger" @click="smoke_end(row)" v-if="row.status == '1'">强制结束</lay-button>
        <!-- <lay-button size="xs" type="" @click="screen(row)" v-if=" title!='评吸烟支设置' && title!='查看任务' && row.status==1">投屏</lay-button> -->
        <lay-button size="xs" type="" v-permissions="'meeting_screen'" @click="detail(row)" v-if="    title!='评吸烟支设置'&& row.status!=0">详情</lay-button>
        <lay-button size="xs" type="" v-permissions="'metting_export'" v-if="title=='查看任务' &&  row.status==2" @click="export_tab(row)">导出任务</lay-button>
      </template>
    </lay-table>

    <!-- 签到表 -->
    <!-- 签到表设置 -->
    <el-dialog :append-to-body="false" ref="myDialog" v-model="dialogVisible" width="100%" :show-close="false">
      <div class="sign-setting">
        <h1 class="df w100 jc-sb">
          {{title == '评吸烟支设置' ? '签到表设置':'查看签到表' }}

          <span style="margin-right: 50px;">
            <el-button @click="sign_close">取消</el-button>
            <el-button type="primary" @click="sign_enter" v-if="title == '评吸烟支设置'">
              保存
            </el-button>
          </span>
        </h1>

        <div>

        </div>
        <div class=" content">
          <table border>
            <tr>
              <td>评吸目的</td>
              <td colspan="4"><input type="text" v-model="signFrom.objective" name="" id="" placeholder="请输入....." v-if="title == '评吸烟支设置'"> <span v-else>{{ signFrom.objective }}</span></td>
            </tr>
            <tr>
              <td>会议组织部门</td>
              <td colspan="2">
                <!-- <lay-select v-if="title == '评吸烟支设置'" v-model="signFrom.orgId" placeholder="请选择....." style="border: 0 none;width: 100%;height: 100%;" size="sm" class="selece">
                  <lay-select-option v-for="item in orgList" :value="item.orgId" :key="item.orgId" :label="item.orgName" @click="org_select(item)"></lay-select-option>
                </lay-select>
                <span v-else>{{ signFrom.orgName }}</span> -->
                <span>{{ signInfo.departName }}</span>
                <!-- <input type="text" v-model="signFrom.orgId" name="" id="" placeholder="请输入....."> -->
              </td>
              <td>会议申请人</td>
              <td>
                <!-- <input v-model="signFrom.host" type="text" name="" id="" placeholder="请输入....."> -->
                <lay-select v-if="title == '评吸烟支设置'" v-model="signFrom.host" placeholder="请选择....." style="border: 0 none;width: 100%;height: 100%;" size="sm" class="selece">
                  <lay-select-option v-for="item in personList" :value="item.description" :key="item.description" :label="item.description" @click="host_select(item)"></lay-select-option>
                </lay-select>
                <span v-else>{{ signFrom.host }}</span>
              </td>
            </tr>
            <tr>
              <td>评吸时间</td>
              <td colspan="2">
                <!-- <input type="text" v-model="signFrom.meetingTime" name="" id="" placeholder="请输入....."> -->
                <!-- <input type="datetime-local"> -->
                <!-- <lay-date-picker type="datetime" v-if="title == '评吸烟支设置'" v-model="signFrom.meetingTime" placeholder="请选择....." size="sm" class="time"></lay-date-picker>
                <span v-else>{{ signFrom.meetingTime }}</span> -->
                <span>{{signInfo.meetime}}</span>
              </td>
              <td>评吸地点</td>
              <td><input type="text" v-if="title == '评吸烟支设置'" v-model="signFrom.location" name="" id="" placeholder="请输入....."> <span v-else>{{ signFrom.location }}</span></td>

            </tr>
            <tr>
              <td>样品种类</td>
              <td colspan="2">单料烟</td>
              <td colspan="2">成品烟</td>
            </tr>

            <tr>
              <td>评吸数量(支)</td>
              <td colspan="2">
                <!-- <input type="text" v-model="signFrom.unblendedAmout" name="" id="" placeholder="请输入....."> -->
                <!-- <el-input v-if="title == '评吸烟支设置'" v-model="signFrom.unblendedAmout" placeholder="请输入整数" /> -->
                <!-- <el-input-number v-if="title == '评吸烟支设置'" v-model="signFrom.unblendedAmout" :min="0" :max="100" :step="1" /> -->
                <!-- {{signFrom.unblendedAmout}} -->
                <span>{{ SmokeNumInfo?.unblended_amout  || 0}}</span>
              </td>
              <td colspan="2">
                <!-- <el-input v-if="title == '评吸烟支设置'" v-model="signFrom.perfectAmount" placeholder="请输入整数" /> -->
                <!-- <el-input-number v-if="title == '评吸烟支设置'" v-model="signFrom.perfectAmount" :min="0" :max="100" :step="1" /> -->
                <!-- <span>{{ signFrom.perfectAmount }}</span> -->
                <span>{{ SmokeNumInfo?.perfect_amount  || 0}}</span>
                <!-- <input type="text" v-model="signFrom.perfectAmount" name="" id="" placeholder="请输入....."> -->
              </td>
            </tr>
            <tr v-if="title != '评吸烟支设置'">
              <td>参加人数</td>
              <td colspan="4">{{signInfolength}}</td>
            </tr>
            <tr v-if="title != '评吸烟支设置'">
              <td :rowspan="signInfolength+1">感官评吸人员签字</td>
              <td>序号</td>
              <td>姓名</td>
              <td>单位/部门</td>
              <td>国评技术岗位证书</td>
            </tr>

            <template v-if="title != '评吸烟支设置'">
              <tr v-for="(item,index) in signInfo.department" :key="index">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.org}}</td>
                <td class="">
                  <div class="df ai-c jc-c w100">

                    <lay-checkbox-group v-model="item.certificate" style="  pointer-events: none; ">
                      <lay-checkbox name="like" skin="primary" value="1">有</lay-checkbox>
                      <lay-checkbox name="like" skin="primary" value="0">无</lay-checkbox> 
                    </lay-checkbox-group>

                    <!-- <lay-radio-group name="action" v-model="item.certificate">
                      <lay-radio value="1">有</lay-radio>
                      <lay-radio value="0">无</lay-radio>
                    </lay-radio-group> -->

                    <!-- <div class="df ai-c"> <lay-radio v-model="item.certificate" name="action" :value="1" label="有"></lay-radio></div>
                    <div class="df ai-c" style="margin-left: 8px;">无<input type="checkbox" name="" id="" disabled></div> -->
                  </div>
                </td>
              </tr>
            </template>
            <tr v-if="title != '评吸烟支设置'">
              <td colspan="5">参加评吸会议感官评吸人员（共 {{signInfolength}}人）</td>
            </tr>
            <tr v-if="title != '评吸烟支设置'">
              <td colspan="5">备注：会议申请人可根据预计到会感官评吸人员人数自行增减表格记录</td>
            </tr>

          </table>
        </div>
      </div>

      <!-- <template #footer>
        <div style="margin-right: 50px;">
          <el-button @click="sign_close">取消</el-button>
          <el-button type="primary" @click="sign_enter">
            保存
          </el-button>
        </div>
      </template> -->
    </el-dialog>

  </div>
</template>

<script setup lang="ts">
import http from "@/http";
import { onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { layer } from "@layui/layui-vue";

import { useCounterStore } from "@/current";
import { storeToRefs } from "pinia"; // cnpm i pinia --save  2.1.3
const store = useCounterStore();
const { nowMenuBtnArr, system } = storeToRefs(store); //  数据

onMounted(async () => {
  await get_mett_detail(mettingId.value);
  if (title.value == "评吸烟支设置") {
    signName.value = "设置签到表";
  } else {
    signName.value = "查看签到表";
  }

  await getSmokeNum();
});
const router = useRouter();
const route = useRoute();
const info = ref<any>([]);
const mettingId = ref(route.query.id);
const title = ref(route.query.title);
const signName = ref("");
const taskArr = ref(["单点评吸", "对比评吸", "三点评吸", "竞品评价", "单料评吸", "对比排序"]);
const dialogVisible = ref(false);
const columns = ref([
  { title: "任务类型", key: "classify", customSlot: "classify" },
  { title: "状态", key: "status", customSlot: "status" },
  { title: "操作", width: "300", customSlot: "operator", key: "operator", fixed: "right", ignoreExport: true },
]);

const dataSource = ref([]);

// 开始评吸、
const start_task = async (item) => {
  console.log(item);

  layer.confirm("确认要开始评吸吗", {
    btn: [
      {
        text: "确认",
        callback: async (closeid) => {
          const res = await http.corsApi(`/smokeMeeting/startSmoke?meetingId=${item.meetingId}&classify=${item.classify}`, "put", {
            meetingId: item.meetingId,
            classify: item.classify,
          });
          if (res.data) {
            layer.msg("开始成功", { icon: 1, time: 1500 });
            await get_mett_detail(mettingId.value);
          }
          console.log(res);

          layer.close(closeid);
        },
      },
      {
        text: "取消",
        callback: (id) => {
          // layer.msg("坐着看");
          layer.close(id);
        },
      },
    ],
  });

  // if (nowId.value == item.id) {
  //   console.log(item);
  //   item.isStatus = "1";
  // }
};

// 结束评吸
const close_px = async () => {
  layer.confirm("确认要结束任务吗", {
    title: "提示",
    btn: [
      {
        text: "确认",
        callback: async (confirmid) => {
          const res = await http.corsApi(`/smokeMeeting/endTask/${mettingId.value}`, "put", { id: mettingId.value });
          console.log(res);
          if (res.data) {
            layer.close(confirmid);
            router.push("/smokePanel/metting");
            layer.msg("结束评吸成功", { time: 1500, icon: 1 });
          }
        },
      },
      {
        text: "取消",
        callback: (id) => {
          layer.close(id);
        },
      },
    ],
  });
};
// 导出
const export_tab = async (row) => {
  console.log(row);
  let title = "";
  let url = "";
  if (row.classify == "0") {
    url = "/meetingMonthly/exportFile";
    title = "单点评吸";
  } else if (row.classify == "1") {
    url = "/meetingContrast/exportFile";
    title = "对比评吸";
  } else if (row.classify == "2") {
    url = "/meetingThreePoint/exportWord";
    title = "三点评吸";
  } else if (row.classify == "3") {
    url = "/meetingCompetitors/exportFile";
    title = "竞品评价";
  } else if (row.classify == "4") {
    url = "/meetingUnblended/exportFile";
    title = "单料评吸";
  } else if (row.classify == "5") {
    // smokeMeeting/contrastSortScreen
    url = "/meetingContrastSort/exportFile";
    title = "对比排序";
  }

  const res = await http.corsApi(`${url}/${row.id}`, "get", {}, { is_msg: false, formData: true });

  // console.log(res);

  if (res.data) {
    // res 为后端返回的响应对象
    // let blob = new Blob([res]);
    let url = window.URL.createObjectURL(res.data);
    let a = document.createElement("a");
    a.href = url;
    a.download = decodeURI(res.headers["content-disposition"]).split("filename=")[1].replace(/"/g, "") || new Date().getTime() + ".zip";
    a.click();
    window.URL.revokeObjectURL(url);
    layer.msg("导出成功", { icon: 1, time: 2000 });
  }
};
// 导出签到表
const exportSign = async () => {
  const res = await http.corsApi(`/meetingSignin/exportFile/${mettingId.value}`, "get", {}, { is_msg: false, formData: true });

  // console.log(res);

  if (res) {
    // res 为后端返回的响应对象
    // let blob = new Blob([res]);
    let url = window.URL.createObjectURL(res.data);
    let a = document.createElement("a");
    a.href = url;
    a.download = decodeURI(res.headers["content-disposition"])?.split("filename=")[1]?.replace(/"/g, "") || new Date().getTime() + ".zip";
    a.click();
    window.URL.revokeObjectURL(url);
    layer.msg("导出成功", { icon: 1, time: 2000 });
  }
};

// 获取签到表成品烟单料烟数量、
const SmokeNumInfo = ref();
const getSmokeNum = async () => {
  const res = await http.corsApi("/meetingSignin/countByMeetingId", "get", { meetingId: mettingId.value });
  console.log(res);
  SmokeNumInfo.value = res.data;
  signFrom.value.perfectAmount = res.data?.perfect_amount || 0;
  signFrom.value.unblendedAmout = res.data?.unblended_amout || 0;
};
const signInfo = ref([]);
const signInfolength = ref(0);
const sign_list = async () => {
  try {
    const res = await http.corsApi(`/smokeMeeting/meetingSignDetail/${mettingId.value}`, "get", {});
    console.log(res);
    signInfo.value = res.data;
    let data = res.data.meetingSigninEntity;
    signInfolength.value = res.data.department ? Object.keys(res.data.department).length : [] || 0;
    console.log(signInfolength.value);

    if (res.data.meetingSigninEntity) {
      signFrom.value = data;
      orgName.value = data.orgName;

      hostName.value = data.hostId;
      signFrom.value.host = data.host;

      console.log(signFrom.value);
    } else {
      console.log(11111111);
      hostName.value = system.value.id;
      signFrom.value.host = system.value.description;

      // signFrom.value.host = system.value.id
      // signFrom.value.hostId =system.value.id
    }
  } catch (err) {
    console.log(err);
  }
};

// 签到表from
const signFrom = ref({
  host: "",
  location: "",
  meetingId: 0,
  meetingTime: "",
  number: "",
  objective: "",
  orgId: "",
  perfectAmount: 0,
  unblendedAmout: 0,
});
// 签到表
const sing_open = () => {
  dialogVisible.value = true;
  get_person();
  sign_list();
  get_org_list_tree();
};
const orgName = ref("");
const org_select = (val) => {
  console.log(val);
  orgName.value = val.orgName;
};
const hostName = ref("");
const host_select = (val) => {
  console.log(val);
  hostName.value = val.name;
};

// 签到表保存
const sign_enter = async () => {
  console.log(info.value);
  console.log(signFrom.value);

  if (signFrom.value.unblendedAmout < 0 || signFrom.value.perfectAmount < 0 || isNaN(signFrom.value.unblendedAmout * 1) || isNaN(signFrom.value.perfectAmount * 1)) {
    layer.msg("请检查单料烟/成品烟数量是否正确", { icon: 2, time: 2000 });
    return false;
  }

  let data = {};
  let method = "";
  console.log(signInfo.value.meetime + ' 00:00:00');
  
  

  // return false
  // 修改
  if (info.value.meetingSigninRep && info.value.meetingSigninRep.id) {
    signFrom.value.hostId = hostName.value;

    data = { ...signFrom.value, meetingId: mettingId.value };
    method = "put";
  } else {
    //新增signInfo
    data = {
      // hostId: signFrom.value.host,
      // host: hostName.value,

      hostId: hostName.value,
      host: signFrom.value.host,

      location: signFrom.value.location,
      meetingId: info.value.id,
      // meetingTime: info.value.meetingTime,
      // meetingTime: signFrom.value.meetingTime,
      meetingTime: signInfo.value.meetime+ ' 00:00:00',

      // number:signFrom.value,
      objective: signFrom.value.objective,
      // orgId: info.value.orgId,
      orgId: signInfo.value.departId,
      perfectAmount: signFrom.value.perfectAmount,
      unblendedAmout: signFrom.value.unblendedAmout,
      // orgName: orgName.value,
      orgName: signInfo.value.departName,
    };
    method = "post";
  }
  console.log(data);

  // return false

  const res = await http.corsApi("/meetingSignin", method, data);
  console.log(res);
  if (res.data) {
    layer.confirm("修改成功", { icon: 1 });
    await get_mett_detail(mettingId.value);
    dialogVisible.value = false;
  }
};
const sign_close = async () => {
  dialogVisible.value = false;
  await get_mett_detail(mettingId.value);
};

// 烟支设置 配置
const smoke_setting = (item: any) => {
  // console.log(item);
  // ?  classify? [0,1,2,43.4] ===> (["单点评吸", "对比评吸", "三点评吸", "竞品评价", "单料评吸"]);
  if (title.value === "评吸烟支设置") {
    // router.push("/smoke/pingxisetting");

    if (item.classify == 0) {
      //月度平息
      router.push("/smoke/month?id=" + item.id);
    } else if (item.classify == 1) {
      //对比评吸
      // top_title.value = '对比平息设置'
      // d_dialogVisible.value = true;
      router.push("/smoke/db?id=" + item.id);
    } else if (item.classify == 2) {
      //三点评吸
      // s_dialogVisible.value = true;
      router.push("/smoke/sd?id=" + item.id);
    } else if (item.classify == 3) {
      //竞价·评吸
      // j_dialogVisible.value = true;
      router.push("/smoke/jp?id=" + item.id);
    } else if (item.classify == 4) {
      //单料评吸
      // dl_dialogVisible.value = true;
      router.push("/smoke/dl?id=" + item.id);
    } else if (item.classify == 5) {
      router.push("/smoke/comRank?id=" + item.id);
    }
  }
};

// 投屏
const screen = (row) => {
  console.log("投屏", row);
  // dialogVisible.value = true;
  // window.open('/screen.html')

  // return false
  let url = "";
  let title = "";
  if (row.classify == "0") {
    url = "/smokeMeeting/monthlyScreen";
    title = "单点评吸完成情况";
  } else if (row.classify == "1") {
    url = "/smokeMeeting/contrastScreen";
    title = "对比评吸完成情况";
  } else if (row.classify == "2") {
    url = "/smokeMeeting/threePointScreen";
    title = "三点评吸完成情况";
  } else if (row.classify == "3") {
    url = "/smokeMeeting/competitorstScreen";
    title = "竞品评价完成情况";
  } else if (row.classify == "4") {
    url = "/smokeMeeting/unblendedScreen";
    title = "单料评吸完成情况";
  } else if (row.classify == "5") {
    // smokeMeeting/contrastSortScreen
    url = "/smokeMeeting/contrastSortScreen";
    title = "对比排序完成情况";
  }

  router.push(`/smokePanel/metting/screen?id=${row.id}&url=${url}&title=${title}`);
};

// 详情
const detail = (row) => {
  console.log("详情", row);
  // dialogVisible.value = true;

  if (row.classify == "0") {
    router.push(`/smokePanel/metting/detail?id=${row.id}&status=${row.status}`);
  } else if (row.classify == "1") {
    router.push(`/smokePanel/metting/db/detail?id=${row.id}&status=${row.status}`);
  } else if (row.classify == "2") {
    // router.push(`/smokePanel/metting/sd/detail?mettingID=${mettingId.value}&id=${row.id}&status=${row.status}&title=${route.query.title}`);
    router.push(`/smokePanel/metting/threeDetail/detail?mettingID=${mettingId.value}&id=${row.id}&status=${row.status}&title=${route.query.title}`);
  } else if (row.classify == "3") {
    router.push(`/smokePanel/metting/jp/detail?mettingID=${mettingId.value}&id=${row.id}&status=${row.status}`);
  } else if (row.classify == "4") {
    router.push(`/smokePanel/metting/dl/detail?mettingID=${mettingId.value}&id=${row.id}&status=${row.status}`);
  } else if (row.classify == "5") {
    router.push(`/smokePanel/metting/comRank/detail?mettingID=${mettingId.value}&id=${row.id}&status=${row.status}`);
  }
};
// 关闭
const close = () => {
  router.push("/smokePanel/metting");
};
// 结束平息
const smoke_end = async (row) => {
  console.log(row);

  layer.confirm("您确定要强制结束当前任务吗？", {
    title: "提示",
    btn: [
      {
        text: "确认",
        callback: async (id) => {
          const res = await http.corsApi(`/smokeMeeting/endOneMeeting/${row.meetingId}/${row.classify}`, "get", {});
          console.log(res);

          if (res.data) {
            layer.msg("结束成功", { icon: 1, time: 1500 });

            await get_mett_detail(mettingId.value);

            layer.close(id);
          }
        },
      },
      {
        text: "取消",
        callback: (id) => {
          layer.close(id);
        },
      },
    ],
  });
};

const orgList = ref([]);
// 获取部门列表
const get_org_list_tree = async () => {
  // const res = await http.corsApi("/secOrg/getOrgAllTreeData", "get", {});
  const res = await http.corsApi("/secOrg", "get", {});
  console.log(res);
  orgList.value = res.data;
};
// 获取会议详情
const get_mett_detail = async (id) => {
  const res = await http.corsApi(`smokeMeeting/${id}`, "get", {}, { is_msg: false });
  console.log(res);
  dataSource.value = res.data.meetingAllRepList;
  info.value = res.data;
};

const personList = ref([]);
// 获取住处人
const get_person = async () => {
  const res = await http.corsApi("/secOrg/orgAndUserName", "get", {});
  console.log(res);
  personList.value = res.data;
};
</script>
 
<style scoped lang="scss">
.setting_new {
  padding: 15px;
  width: 100%;
  height: 92%;
  position: relative;
  box-sizing: border-box;
  &_top_operator {
    padding: 15px 0;
    box-sizing: border-box;
    .layui-btn {
      padding: 0 20px !important;
    }
  }
  .tab_top_btn {
    .layui-btn {
      padding: 0 20px !important;
    }
  }
}

.sign-setting {
  width: 100%;
  height: 100%;
  h1 {
    background-color: #fff;
    font-size: 24px;
    padding: 20px 0 80px 30px;
    box-sizing: border-box;
    box-shadow: 0 0 10px #ccc;
    color: #000;
  }

  .content {
    background-color: #f0f2f5;
    width: 100%;
    //  height: 100%;
    min-height: 100%;
    padding-top: 15px;
    padding: 10px 30px;
    box-sizing: border-box;
    table {
      width: 70%;
      text-align: center;
      background-color: #fff;
      tr {
        td {
          padding: 8px 12px;
          width: 200px;
          input {
            width: 100%;
            height: 100%;
            border: 0 none;
            text-align: center;
          }
          :deep(.layui-form-checkbox) {
  cursor: not-allowed;
}
        }
      }
      .selece {
        :deep(.layui-input) {
          border: 0 none;
          .layui-icon {
            display: none;
          }
          input {
            text-align: center;
          }
        }
      }
      .time {
        :deep(.layui-input) {
          border: 0 none;
          .layui-icon {
            display: none;
          }
          input {
            text-align: center;
          }
        }
      }
    }
  }
}

:deep(.el-overlay-dialog) {
  position: absolute !important;
}
:deep(.el-overlay) {
  position: absolute !important;
}
:deep(.el-input__wrapper) {
  box-shadow: none;
}

:deep(.el-dialog) {
  width: 100%;
  height: 100%;
  height: calc(100% - 0px);
  margin: 0;
  padding: 0;
}
:deep(.el-dialog__body) {
  height: 79%;
}


</style>